<?php

class Login extends Controller {

	function Login()
	{
		parent::Controller();
		$this->load->helper(array('url'));	
		$this->load->library('session');
	}
	
	function index()
	{
		$this->load->view('login/login_box');
	}
	
	function sign_in()
	{
		$mail = $_POST['login_mail'];
		$password = sha1($_POST['login_password']);

		if($mail && $password)
		{
			try{
				$this->load->model('Login_model', '', TRUE);
				$user = $this->Login_model->check_login($mail,$password);
				
				if(isset($user))
				{
					//load the user in the session
					$data_user = array();
					foreach($user as $key => $field)
					{
						$data_user[$key] = $field;
					}
					
					$this->session->set_userdata("user_logged_in",$data_user);				
				}		

				//redirection after login
				if($this->session->flashdata('link_redirect'))
					$link = $this->session->flashdata('link_redirect');
				else
					$link = site_url();
				redirect($link);				
			}
			catch(Exception $e)
			{
				$this->session->set_flashdata('msg','We could not log you in, wrong password or e-mail address');
				
				if($this->session->flashdata('link_redirect'))
					$this->session->keep_flashdata('link_redirect');
				
				//redirect to the previous page
				header("Location: ".$_SERVER['HTTP_REFERER']."");
			}
			

		}
	

	}
	
}
?>
